package com.efast.dmom.aps.datahandlers;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.Shift;
import com.efast.dmom.aps.bean.WorkingCalendar;
import com.efast.dmom.aps.dao.WorkingCalendarDao;
import com.efast.dmom.aps.dao.WorkingCalendarShiftDao;
import com.efast.dmom.aps.entity.WorkingCalendarEntity;
import com.efast.dmom.aps.util.TimeUtil;
@Component
public class WorkCalendarDataHandler {
	
	@Autowired
	private WorkingCalendarDao workingCalendarDao;
	
	@Autowired
	private WorkingCalendarShiftDao wcsDao;
	
	@Autowired
	private ShiftDataHandler shiftDataHandler;
	
	public List<WorkingCalendar> selectcalendarsAndSchedulingCount(WorkingCalendar workingCalendar) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startDate", workingCalendar.getCalendarStartDate().toString());
		map.put("endDate", workingCalendar.getCalendarEndDate().toString());
		map.put("plantId", workingCalendar.getPlantId());
		map.put("workshop", workingCalendar.getWorkshopId());
		map.put("productionLineId", workingCalendar.getLineId());
		List<WorkingCalendar> selectcalendarsAndSchedulingCount = workingCalendarDao.selectcalendarsAndSchedulingCount(map);
		return selectcalendarsAndSchedulingCount;
		
	}

	public List<WorkingCalendar> getWorkingHoursPerDay(Map<String,Object> map) {
		return workingCalendarDao.getWorkingHoursPerDay(map);
	}
	
	public List<Map<String,Object>> queryShiftInfoByCalendar(WorkingCalendar workingCalendar) {
		List<Map<String,Object>> queryShiftInfoByCalendar = workingCalendarDao.queryShiftInfoByCalendar(workingCalendar);
		return queryShiftInfoByCalendar;
	}
	
	public List<Map<String,Object>> workCalendarDataHandler(Map<String,Object> map){
		List<Map<String, Object>> workCalendarDataHandler = workingCalendarDao.workCalendarDataHandler(map);
		return workCalendarDataHandler;
		
	}
	
	public void updateCalendarStatus(Map<String, Object> map) {
		workingCalendarDao.updateCalendarStatus(map);
	}
	
	public void insertCalendarDatas(Map<String,Object> map) {
		map.put("siteId",DataHandlerUtils.getSiteId());
		map.put("creationTime",TimeUtil.getLocalDateTime());
		map.put("creationTimeZone", TimeUtil.getLocalTimeZone());
		map.put("creator", SpringWebUtils.getRequestUser());
		map.put("lastModifiedTime", TimeUtil.getLocalDateTime());
		map.put("lastModifiedTimeZone", TimeUtil.getLocalTimeZone());
		map.put("lastModifier", SpringWebUtils.getRequestUser());
		map.put("rowStatus", 1);
		map.put("rowTrxId", 111);
		workingCalendarDao.insertCalendarDatas(map);
	}
	
	// 通过车间产线工厂日历时间查询对应的日历id
	public List<Long> queryCalendarByPlantWorkshopLine (Map<String,Object> map) {
		List<Long> queryCalendarByPlantWorkshopLine = workingCalendarDao.queryCalendarByPlantWorkshopLine(map);
		return queryCalendarByPlantWorkshopLine;
	}
	
	// 新增工作日历和班次关系
	public void addCalendarShiftRel (Map<String,Object> map) {
		map.put("siteId",DataHandlerUtils.getSiteId());
		map.put("creationTime()",TimeUtil.getLocalDateTime());
		map.put("creationTimeZone", TimeUtil.getLocalTimeZone());
		map.put("creator", SpringWebUtils.getRequestUser());
		map.put("lastModifiedTime", TimeUtil.getLocalDateTime());
		map.put("lastModifiedTimeZone", TimeUtil.getLocalTimeZone());
		map.put("lastModifier", SpringWebUtils.getRequestUser());
		map.put("rowStatus", 1);
		map.put("rowTrxId", 111);
		workingCalendarDao.addCalendarShiftRel(map);
	}
	
	public void deleteCalendarShiftRel (Map<String,Object> map) {
		workingCalendarDao.deleteCalendarShiftRel(map);
	}
	
	public List<Map<String,Object>> selectCalendarDataByCalendarDateList(Map<String,Object> map){
	 return	workingCalendarDao.selectCalendarDataByCalendarDateList(map);
	}
	
	public Shift getShiftByLineAndTime(long lineId,LocalDate date,LocalTime time) {
		
		return null;
	}
	public List<Shift> getShiftByLineAndTime(long lineId,LocalDate date) {
		 WorkingCalendarEntity wCalendar = workingCalendarDao.getWcByLineAndTime(lineId, date);
		if(wCalendar==null) {
			return null;
		}
		int dateType = wCalendar.getDateType();
		//休息
		if(dateType==1){
			return null;
		}
		long id = wCalendar.getId();
		List<Long> shiftIds = wcsDao.getShiftByWcId(id);
		List<Shift> shifts =new ArrayList<Shift>();
		for(long sid:shiftIds) {
			shifts.add(shiftDataHandler.getById(sid));
		}
		
		return shifts;
	}
}
